package com.topboya.linker.ui.presenter;

import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.LatelyDataResponse;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.ProduceInfoView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ProduceInfoPresenter extends BasePresenter<ProduceInfoView> {

    private static final String TAG = "CustomerPresenter-";
    public ProduceInfoPresenter(BaseActivity context) {
        super(context);

    }

    public void getProductInforList(String pageNo,String productName,String extendFlag) {

        LogUtils.v(TAG + "pageNo----------->"+pageNo);
        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getProductInforList(pageNo,productName,extendFlag)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(productInforListReponse -> {
                    LogUtils.v(TAG + " result:" + productInforListReponse.toString());
                    mContext.hideWaitingDialog();
                    String success = productInforListReponse.getMeta().getSuccess();
                    if (null != success && Boolean.parseBoolean(success)) {
                        ArrayList<ProductInforListReponse.ProductDetailInfoList>    productDetailInfoLists= (ArrayList<ProductInforListReponse.ProductDetailInfoList>) productInforListReponse.getData().getDocDetailInfoList();
                        String count = productInforListReponse.getData().getCount();
                        getView().setProductInfoCustomeData(productDetailInfoLists,count);
                    }


                }, this::getOrgError);

    }

    public void getLatelyData() {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getLatelyData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(latelyDataResponse -> {
                    LogUtils.v(TAG + " result:" + latelyDataResponse.toString());
                    mContext.hideWaitingDialog();
                    String success = latelyDataResponse.getMeta().getSuccess();
                    if (null != success && Boolean.parseBoolean(success)) {

                       List<LatelyDataResponse.Data> data = latelyDataResponse.getData();
                        getView().setLatelyData(data);
                    }


                }, this::getOrgError1);

    }





    private void getOrgError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "getOrgError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }

    private void getOrgError1(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "getOrgError1:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }

}
